package review;

import lev01_beginnerClass.util.HelpUtil;

import java.util.Arrays;

public class Review03 {
    public static void main(String[] args) {
        for(int i = 0;i < 5000;i++){
            int[] arr = HelpUtil.generateRandomArr(10, 20);
            Arrays.sort(arr);
            int target = (int)(Math.random()*20);
            if(findrightmostless(arr,target) != fortest03(arr,target)){
                System.out.println(Arrays.toString(arr));
                System.out.println(target);
                System.out.println(findrightmostless(arr, target));
                System.out.println(fortest03(arr,target));
                break;
            }
        }


    }

    public static boolean findTarget(int[] arr,int target){
        if(arr == null){
            return false;
        }
        int left = 0;int right = arr.length-1;
        while(left<= right){
            int mid = left + ((right-left)>>1);
            if(arr[mid] == target){
                return true;
            }else if(arr[mid] < target){
                left = mid+1;
            }else {
                right = mid -1;
            }
        }
        return false;
    }

    public static boolean fortest01(int[] arr,int target){
        for(int i = 0; i < arr.length;i++){
            if(arr[i] == target){
                return true;
            }
        }
        return false;
    }

    public static int findleftmostnoless(int[] arr,int target){
        if(arr == null) {
            return -1;
        }
        int left = 0;int right = arr.length-1;
        int ans = -1;
        while(left <= right){
            int mid = left+((right-left)>>1);
            if(arr[mid] >= target){
                ans = mid;
                right = mid -1;
            }else {
                left = mid+1;
            }
        }
        return ans;
    }

    public static int fortest02(int[] arr,int target){
        if(arr == null){
            return -1;
        }
        for(int i = 0; i< arr.length;i++){
            if(arr[i] >= target){
                return i;
            }
        }
        return -1;

    }


    public static int findrightmostless(int[] arr,int target){
        if(arr == null){
            return -1;
        }
        int left = 0;int right = arr.length-1;
        int ans = -1;
        while(left <= right){
            int mid = left+((right - left) >> 1);
            if(arr[mid] <= target){
                ans = mid;
                left = mid +1;
            }else {
                right = mid -1;
            }
        }
        return ans;
    }


    public static int fortest03(int[] arr,int target){
        if(arr == null){
            return -1;
        }
        for(int i = arr.length-1;i>=0;i--){
            if(arr[i] <= target){
                return i;
            }
        }
        return -1;
    }
}
